import _ from 'lodash';

export default {
    namespace: 'patients',
    state: {
        data: [
            {
                id: '000001',
                code: '236982',
                name: '张先聚',
                age: '55岁',
                birthday: '1939-08-22',
                idCard: '',
                gender: '1',
                bedNumber: '002',
                entryDate: '2017-01-03',
                reasonName: '胃溃疡',
                departmentId: 'dpm001',
                departmentName: '消化内科',
                cost: '自费',
                insuranceType: '新农合',
                insuranceCode: '',
                marriage: '已婚',
                nation: '汉族',
                citizenship: 'CN',
                job: '职员',
                organization: '郑州商业大厦',
                connect: '张连西',
            }, {
                id: '000002',
                code: '223421',
                name: '何宗会',
                age: '62岁',
                birthday: '1939-08-22',
                idCard: '',
                gender: '1',
                bedNumber: '039',
                entryDate: '2017-01-08',
                reasonName: '进食哽咽待检查',
                departmentId: 'dpm001',
                departmentName: '消化内科',
                cost: '资费',
                insuranceType: '新农合',
                insuranceCode: '',
                marriage: '已婚',
                nation: '汉族',
                citizenship: 'CN',
                job: '职员',
                organization: '郑州商业大厦',
                connect: '张连西',
            }, {
                id: '000003',
                code: '987302',
                name: '陈强',
                age: '39岁',
                birthday: '1939-08-22',
                idCard: '',
                gender: '1',
                bedNumber: '027',
                entryDate: '2016-12-12',
                reasonName: '胃出血',
                departmentId: 'dpm001',
                departmentName: '消化内科',
                cost: '资费',
                insuranceType: '新农合',
                insuranceCode: '',
                marriage: '已婚',
                nation: '汉族',
                citizenship: 'CN',
                job: '职员',
                organization: '郑州商业大厦',
                connect: '张连西',
            }, {
                id: '000004',
                code: '473462',
                name: '丁晓东',
                age: '23岁',
                birthday: '1939-08-22',
                idCard: '',
                gender: '1',
                bedNumber: '039',
                entryDate: '2016-11-29',
                reasonName: '胃酸',
                departmentId: 'dpm001',
                departmentName: '消化内科',
                cost: '资费',
                insuranceType: '新农合',
                insuranceCode: '',
                marriage: '已婚',
                nation: '汉族',
                citizenship: 'CN',
                job: '职员',
                organization: '郑州商业大厦',
                connect: '张连西',
            }, {
                id: '000005',
                code: '548203',
                name: '刘长富',
                age: '77岁',
                birthday: '1939-08-22',
                idCard: '',
                gender: '1',
                bedNumber: '026',
                entryDate: '2016-10-16',
                reasonName: '胫骨骨折',
                departmentId: 'dpm002',
                departmentName: '骨伤科',
                cost: '资费',
                insuranceType: '新农合',
                insuranceCode: '',
                marriage: '已婚',
                nation: '汉族',
                citizenship: 'CN',
                job: '职员',
                organization: '郑州商业大厦',
                connect: '张连西',
            }, {
                id: '000006',
                code: '548203',
                name: '张文文',
                age: '16岁',
                birthday: '1939-08-22',
                idCard: '',
                gender: '0',
                bedNumber: '017',
                entryDate: '2016-09-01',
                reasonName: '胸骨矫型',
                departmentId: 'dpm002',
                departmentName: '骨伤科',
                cost: '资费',
                insuranceType: '新农合',
                insuranceCode: '',
                marriage: '已婚',
                nation: '汉族',
                citizenship: 'CN',
                job: '职员',
                organization: '郑州商业大厦',
                connect: '张连西',
            }
        ],
        departments: [
            {
                label: '消化内科',
                value: 'dpm001',
            },
            {
                label: '骨伤科',
                value: 'dpm002',
            }
        ],
        filter: {
            key: null,
            departmentId: null,
            departmentName: null,
        },
    },
    reducers: {
        setFilter(state, { payload: id }) {
            const { value: departmentId, label: departmentName } = _.find(state.departments, ['value', id]);
            return {
                ...state,
                filter: {
                    ...state.filter,
                    departmentId,
                    departmentName,
                },
            };
        },
    },
    effects: {
        *init({ payload }, { put, select }) {
            const {
                filter: { departmentId },
                departments
            } = yield select((state) => state.patients);
            if (!departmentId) {
                if (departments.length > 0) {
                    const { value: departmentId } = departments[0];
                    yield put({
                        type: 'setFilter',
                        payload: departmentId,
                    });
                }
            }
        },
    },
    subscriptions: {
        setup(args) {
            const { dispatch, history } = args;
            history.listen(location => {
                if (location.pathname === '/home/check') {
                    dispatch({
                        type: 'init',
                    });
                }
            });
        }
    },
};
